package com.woniu.zsd.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.zsd.platform.dao.AccountRoleMapper;
import com.woniu.zsd.platform.dao.RoleMapper;
import com.woniu.zsd.platform.dao.RoleResourceMapper;
import com.woniu.zsd.platform.dto.ResourceDto;
import com.woniu.zsd.platform.form.RoleResourceForm;
import com.woniu.zsd.platform.model.AccountRole;
import com.woniu.zsd.platform.model.Resource;
import com.woniu.zsd.platform.dao.ResourceMapper;
import com.woniu.zsd.platform.model.Role;
import com.woniu.zsd.platform.model.RoleResource;
import com.woniu.zsd.platform.service.ResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@Service("palteformResourceService")
@Primary
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {
    @javax.annotation.Resource
    private RoleMapper roleMapper;

    @javax.annotation.Resource
    private ResourceMapper resourceMapper;
    @javax.annotation.Resource
    private RoleResourceMapper roleResourceMapper;
    @javax.annotation.Resource
    private AccountRoleMapper accountRoleMapper;

    @Override
    public void saveRoleResource(RoleResourceForm param) throws Exception {
        //获取角色id
        Integer roleId = param.getRoleId();
        //判断角色是否存在
        Role role = roleMapper.selectById(roleId);
        if(role==null){
            throw new Exception("角色不存在");
        }
        //删除该角色权限
        QueryWrapper<RoleResource> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        roleResourceMapper.delete(wrapper);
        //为角色添加新的权限
        List<Integer> resourceIds = param.getResourceIds();
        for(Integer resourceId : resourceIds){
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceId);
            roleResourceMapper.insert(roleResource);
        }
    }

    @Override
    public List<ResourceDto> getAllResource() throws Exception {

        ArrayList<ResourceDto> resourceList = new ArrayList<>();
        // 获取所有树形资源
        List<Resource> resources = resourceMapper.selectList(new QueryWrapper<Resource>().eq("parent_id", 0));
        for (Resource resource : resources) {
            // 将Resource对象转换为ResourceDto
            ResourceDto resourceDto = BeanUtil.toBean(resource, ResourceDto.class);
            // 递归查找子资源并赋值
            findChildren(resourceDto);
            resourceList.add(resourceDto);
        }

        return resourceList;
    }

    private void findChildren(ResourceDto resourceDto) throws Exception {
        // 获取当前资源的所有子资源
        List<Resource> resources = resourceMapper.selectList(new QueryWrapper<Resource>().eq("parent_id", resourceDto.getResourceId()));

        // 遍历子资源，转换为DTO并加入children集合
        for (Resource resource : resources) {
            ResourceDto childResourceDto = BeanUtil.toBean(resource, ResourceDto.class);
            // 将子资源添加到当前资源的children中
            resourceDto.getChildren().add(childResourceDto);
            // 递归查找子资源的子资源
            findChildren(childResourceDto);
        }
    }


    @Override
    public List<Resource> getRoleResource(Integer roleId) throws Exception {
        //根据用户id获取角色id
//        AccountRole accountRole = accountRoleMapper.selectOne(new QueryWrapper<AccountRole>().eq("account_id", accountId));
//        if(accountRole==null){
//            throw new Exception("用户角色不存在");
//        }
//        Integer roleId = accountRole.getRoleId();

        //判断角色是否存在
        Role role = roleMapper.selectById(roleId);
        if(role==null){
            throw new Exception("角色不存在");
        }
        ArrayList<Resource> list = new ArrayList<>();
        //通过角色id获取角色权限
        QueryWrapper<RoleResource> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        List<RoleResource> roleResources = roleResourceMapper.selectList(wrapper);
        if(roleResources.size()==0){
            return list;
        }
        //获取权限列表
        List<Integer> resourceIds = roleResources.stream().map(RoleResource::getResourceId).collect(Collectors.toList());
        List<Resource> resources = resourceMapper.selectBatchIds(resourceIds);
        list.addAll(resources);
        return list;
    }

}
